import pyecharts.options as opts
from pyecharts.charts import Kline, Line


def draw_yield_line(data_frame, roa, max_draw_down, sharp_ratio):
    """
    依据买卖后的总回报率、最大回撤、夏普比率等数据，作出"收益率曲线图.html"
    :param data_frame:依据策略进行买卖后，dataframe（index:日期，columns：累计收益率）
    :param roa: 总回报率
    :param max_draw_down: 最大回撤
    :param sharp_ratio: 夏普比率
    :return: "收益率曲线图.html"
    """
    subtitle = f'总收益率：{roa} 最大回撤率：{max_draw_down} 夏普比率：{sharp_ratio}'
    chart_name = 'cumulative_return.html'
    date_list = data_frame.index.tolist()
    data_list = data_frame['cumulative_return'].values.tolist()
    c = (
        Line()
            .add_xaxis(date_list)
            .add_yaxis(
            series_name="收益率",
            y_axis=data_list,
            is_smooth=True,
            label_opts=opts.LabelOpts(is_show=False),
            linestyle_opts=opts.LineStyleOpts(width=2),
            markline_opts=opts.MarkLineOpts(
                data=[opts.MarkLineItem(symbol="none", x="110%", y=0)])
        )
            .set_global_opts(
            title_opts=opts.TitleOpts(title="收益率曲线",
                                      subtitle=subtitle),
            xaxis_opts=opts.AxisOpts(axisline_opts=opts.AxisLineOpts(is_on_zero=False)),
            yaxis_opts=opts.AxisOpts(is_scale=True),
            datazoom_opts=opts.DataZoomOpts(type_="inside")
        )
            .render("../charts/" + chart_name)
    )
    return chart_name


def draw_kline(data_frame):
    """
    依据日频信息，作单只股票日k线图'kline.html'
    :param data_frame: dataframe（index:股票代码，columns：最高价、最低价、开盘价、收盘价）
    :return:日K线图'kline.html'
    """
    chart_name = 'kline.html'
    date_list = data_frame.index.tolist()
    data_list = data_frame[['open', 'close', 'high', 'low']].values.tolist()
    kline = (
        Kline(init_opts=opts.InitOpts(width="800px", height="500px"))
            .add_xaxis(date_list)
            .add_yaxis("kline",
                       data_list,
                       itemstyle_opts=opts.ItemStyleOpts(
                           color="#ec0000",
                           color0="#00da3c",
                           border_color="#8A0000",
                           border_color0="#008F28",
                       ))
            .set_global_opts(
            yaxis_opts=opts.AxisOpts(is_scale=True),
            xaxis_opts=opts.AxisOpts(is_scale=True),
            title_opts=opts.TitleOpts(title="日K图"),
            datazoom_opts=opts.DataZoomOpts(type_="inside")
        )

    )
    kline.render("../charts/" + chart_name)
    return chart_name
